package com.flash.DayProcessing.controller;

import com.flash.BusinessData.entity.DealData;
import com.flash.DayProcessing.entity.AssetValuationMjoinS;
import com.flash.DayProcessing.entity.assetValuationDate;
import com.flash.DayProcessing.mapper.assetValuationMapper;
import com.flash.DayProcessing.service.AssetValuationService;
import com.flash.GlobalManagement.util.*;
import com.flash.InventoryManagement.entity.CashAccrualStock;
import com.flash.InventoryManagement.entity.SecurityAccrualStock;
import com.flash.InventoryManagement.entity.TaStock;
import com.flash.InventoryManagement.service.CashAccrualStockService;
import com.flash.InventoryManagement.service.SecurityAccrualStockService;
import com.flash.TAManagement.entity.Ta;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AssetValuationController {
    @Resource
    AssetValuationService assetValuationService;

    @Resource
    assetValuationMapper assetValuationMapper;

    @Resource
    SecurityAccrualStockService securityAccrualStockService;

    @Resource
    CashAccrualStockService cashAccrualStockService;
    @Resource
    Dbutil dbutil;

    /**
     * 资产估值查询方法
     * @return
     */
    @RequestMapping("selectAssetValuationData")
    public HashMap selectAssetValuationData() {
        List<assetValuationDate> assetValuationDataList = assetValuationService.selectTable();
        HashMap assetValuationDataMap = new HashMap();
        assetValuationDataMap.put("code",0);
        assetValuationDataMap.put("msg","");
        assetValuationDataMap.put("count",null);
        assetValuationDataMap.put("data",assetValuationDataList);
        System.out.println(assetValuationDataMap);
        return assetValuationDataMap;
    }
    @RequestMapping("selectStockarket")
    public void selectStockarket(String toDay,String arrJson,HttpServletRequest  request){
        System.out.println("SS: "+arrJson );
        System.out.println("A: "+toDay);
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        System.out.println("fundId" + fundId);
        List<AssetValuationMjoinS> assetValuationMjoinS = assetValuationService.selectStockarket(toDay, fundId);
        System.out.println("cccccc"+assetValuationMjoinS);
        List<assetValuationDate> assetValuationDatesList = SysUtil.jsonToArrayList(arrJson, assetValuationDate.class);
        for (assetValuationDate assetValuationDate:assetValuationDatesList) {

            if (assetValuationDate.getAssetValuationType().equals("证券估值增值")){

                System.out.println("123"+assetValuationDatesList);
                for (AssetValuationMjoinS assetValuationMjoin:assetValuationMjoinS
                     ) {
                    SecurityAccrualStock securityAccrualStock =  new SecurityAccrualStock();
                    //证券估值
                    //债券应收应付ID
                    securityAccrualStock.setSecurityAccrualStockId(NumberUtil.getNumber(TableIdUtil.SASID, TableNameUtil.SAS, "SAS"));
                    //基金ID
                    securityAccrualStock.setFundId((String) session.getAttribute("fundId"));
                    //证券Id
                    securityAccrualStock.setSecurityId((assetValuationMjoin.getSecurityId()));
                    //时间
                    securityAccrualStock.setDateAccrualStock(toDay);
                    //业务状态
                    securityAccrualStock.setBusinessState(1);
                    //证券类型
                    securityAccrualStock.setSecurityType(1);
                    //金额
                    securityAccrualStock.setMoney(assetValuationMjoin.getTootaIPrice());
                    System.out.println("金额为"+assetValuationMjoin.getTootaIPrice());
                    //初期状态
                    securityAccrualStock.setInitialState(2);
                    //备注
                    securityAccrualStock.setRemark(" ");
                    System.out.println("22222222222"+securityAccrualStock);
                    int i = assetValuationService.delectSecurityAccrualStock(securityAccrualStock);
                    System.out.println("删除行数"+i);
                    if (i==0 || i==1){
                        securityAccrualStockService.insertSecurityAccrualStock(securityAccrualStock);
                        System.out.println("成功");
                    }

                }
            }else {
                System.out.println("=====清算款=====");
                List<DealData> dealData = assetValuationService.selectTransactionData(toDay,fundId);
                System.out.println("时间"+toDay);
                System.out.println("11111"+dealData);
                for (DealData DealDatalist:dealData
                     ) {
                    SecurityAccrualStock securityAccrualStock =  new SecurityAccrualStock();
                    // 清算款
                    //债券应收应付ID
                    securityAccrualStock.setSecurityAccrualStockId(NumberUtil.getNumber(TableIdUtil.SASID, TableNameUtil.SAS, "SAS"));
                    //基金ID
                    securityAccrualStock.setFundId((String) session.getAttribute("fundId"));
                    //证券Id
                    securityAccrualStock.setSecurityId(DealDatalist.getSecurityId());
                    //时间
                    securityAccrualStock.setDateAccrualStock(toDay);
                    //业务状态
                    securityAccrualStock.setBusinessState(1);
                    //证券类型
                    securityAccrualStock.setSecurityType(2);
                    //总金额
                    securityAccrualStock.setMoney(DealDatalist.getEndMoney());
                    //初期状态
                    securityAccrualStock.setInitialState(2);
                    //备注
                    securityAccrualStock.setRemark(" ");

                    System.out.println("实体类"+securityAccrualStock);
                    int i = assetValuationService.delectSecurityAccrualStock(securityAccrualStock);
                    System.out.println("删除行数"+i);
                        securityAccrualStockService.insertSecurityAccrualStock(securityAccrualStock);
                        System.out.println("成功");

                    System.out.println("TA清算款=========================== ");
                    List<Ta> tas = assetValuationService.selectTa(toDay,fundId);
                    System.out.println("121212"+toDay);
                    System.out.println("2211"+tas);
                    CashAccrualStock cashAccrualStock = new CashAccrualStock();
                    for (Ta ta:tas
                         ) {
                        cashAccrualStock.setCashAccrualStockId(NumberUtil.getNumber(TableIdUtil.CASID, TableNameUtil.CAS, "CAS"));
                        //时间
                        cashAccrualStock.setDateAccrualStock(ta.getDealTime());
                        //现金ID
                        cashAccrualStock.setCashId(ta.getCashId());
                        //金额
                        cashAccrualStock.setMoney((double) ta.getMoney());
                        //业务类型
                        cashAccrualStock.setBusinessType(4);
                        //业务状态
                        cashAccrualStock.setBusinessState(1);
                        //初期状态
                        cashAccrualStock.setInitialState(2);
                        //备注
                        cashAccrualStock.setRemark(" ");
                        System.out.println(cashAccrualStock+"hahah");
                    }
                    int i1 = assetValuationService.delectCashAccrualStock(cashAccrualStock);
                    System.out.println(i1+"qwe");
                    cashAccrualStockService.insertCashAccrualStock(cashAccrualStock);
                }
            }
        }

    }
}